var http = require("http");
var fs = require("fs");
var path = require("path");

http.createServer((req, res) => {
    var arg = process.argv[2];
    if(arg != null){
        fs.exists(arg, function(exists){
            if(exists){
                var filePath = path.join(__dirname, arg);
                var fd = fs.openSync(filePath, "r");
                var buf = Buffer.alloc(10);
                var result = fs.readSync(fd, buf);
                res.end(result);
                fs.close(fd);
            }
            if(!exists){
                console.log("文件不存在")
            }
        })
    }else{
        var filePath1 = path.join(__dirname, "/fileReader1.js");
        var fd1 = fs.openSync(filePath1, "r");
        var buf = Buffer.alloc(10);
        var result1 = fs.readSync(fd1, buf);
        res.end(result1);
        fs.close(fd1);
    }
}).listen(8081);

console.log("server is listening 8081")